package com.guigu.buy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.buy.dto.MenuDto;
import com.guigu.buy.pojo.SysMenu;
import com.guigu.buy.pojo.SysMenuRole;
import com.guigu.buy.pojo.SysUser;
import com.guigu.buy.service.ISysMenuRoleService;
import com.guigu.buy.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.awt.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com.guigu
 * @since 2024-05-15
 */
@Controller
@CrossOrigin
@RequestMapping("/buy/sysMenu")
public class SysMenuController {
    @Autowired
    private ISysMenuRoleService iSysMenuRoleService ;
    @Autowired
    private ISysMenuService iSysMenuService;
    @RequestMapping("/getmenu")
    @ResponseBody
    public List<Map<String,Object>> getMenus(@RequestBody SysUser user){
        List<SysMenu> menuList = iSysMenuRoleService.getMenuList(user.getLoginName(),user.getPassword());
        List<Map<String,Object>> list = new ArrayList<>();
        if (menuList.size()>0){
            for(SysMenu menu:menuList){
                Map<String,Object> map = new LinkedHashMap<>();
                map.put("id",menu.getId());
                map.put("name",menu.getName());
                map.put("iamge_url",menu.getIamgeUrl());
                map.put("children",iSysMenuRoleService.gettwoMenuList(user.getLoginName(),user.getPassword(),menu.getId()));
                list.add(map);
                System.out.println(list);
            }
        }
        return list;
    }
    @RequestMapping("/getAllMenu")
    @ResponseBody
    public List<SysMenu> getAllMenu(){
        QueryWrapper<SysMenu> qw =new QueryWrapper<>();
        qw.eq("status",0);
        List<SysMenu> menuList=iSysMenuService.list(qw);
        return menuList;
    }
    @RequestMapping("/getAllKeys/{roleId}")
    @ResponseBody
    public List<SysMenu> getAllKeys(@PathVariable int roleId){
        List<SysMenu> byRoleId = iSysMenuService.getByRoleId(roleId);
        System.out.println(byRoleId);
        return byRoleId;
    }
    @RequestMapping("/updateMenu")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public void updateMenu(@RequestBody MenuDto dto) {
        List<SysMenuRole> menuRolesToSave = new ArrayList<>();
        iSysMenuRoleService.delMenu(dto.getRoleId());
        System.out.println(dto);
        for (int i = 0; i < dto.getSelectedKeys().size(); i++) {
            SysMenuRole mr = new SysMenuRole();
            mr.setMenuId(dto.getSelectedKeys().get(i));
            mr.setRoleId(dto.getRoleId());
            menuRolesToSave.add(mr);
        }
        boolean save = iSysMenuRoleService.saveBatch(menuRolesToSave);
        // 可选地，您可以检查保存操作的结果
        if (!save) {
            throw new RuntimeException("保存菜单角色失败");
        }




}
}
